package com.miao.edu.ad.remote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miao.edu.ad.entity.PromotionAd;
import com.miao.edu.ad.entity.PromotionSpace;
import com.miao.edu.ad.service.IPromotionAdService;
import com.miao.edu.ad.service.IPromotionSpaceService;
import com.miao.edu.api.AdRemoteService;
import com.miao.edu.common.entity.ResponseDTO;
import com.miao.edu.common.utils.ConvertUtil;
import com.miao.edu.dto.PromotionAdDTO;
import com.miao.edu.dto.PromotionSpaceDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author niemiao
 */
@RefreshScope  // 在 Spring Cloud 中，提供了 @RefreshScope 注解，可以声明在 Bean 上，实现该 Bean 的配置刷新。
@RestController
@RequestMapping("/ad")
public class AdRemoteServiceImpl implements AdRemoteService {

    @Value("${ad.flag}")
    private String flag;

    @Autowired
    private IPromotionAdService promotionAdService;

    @Autowired
    private IPromotionSpaceService promotionSpaceService;

    @Override
    @GetMapping("/space/getAllSpaces")
    public  List<PromotionSpaceDTO> getAllSpaces() {
        List<PromotionSpace> list = promotionSpaceService.list();
        System.out.println(flag);
        return ConvertUtil.convertList(list, PromotionSpaceDTO.class);
    }

    @Override
    @GetMapping("/ad/getAdByKey")
    public List<PromotionSpaceDTO> getAdByKey(@RequestParam("keys") String[] keys) {

        List<PromotionSpaceDTO>  promotionSpaceList = new ArrayList<>();

        Arrays.stream(keys).forEach(
                item -> {
                    QueryWrapper<PromotionSpace> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("spaceKey",item);
                    PromotionSpace one = promotionSpaceService.getOne(queryWrapper);
                    QueryWrapper<PromotionAd> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper.eq("spaceId",one.getId());
                    List<PromotionAd> list = promotionAdService.list(queryWrapper2);
                    List<PromotionAdDTO> promotionAdDTOList = ConvertUtil.convertList(list, PromotionAdDTO.class);
                    PromotionSpaceDTO promotionSpaceDTO = ConvertUtil.convert(one, PromotionSpaceDTO.class);
                    promotionSpaceDTO.setPromotionList(promotionAdDTOList);
                    promotionSpaceList.add(promotionSpaceDTO);
                }
        );
        return promotionSpaceList;
    }

    @Override
    @GetMapping("/ad/{id}")
    public PromotionSpaceDTO getPromotionSpaceById(@PathVariable("id") String id) {

        QueryWrapper<PromotionSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        PromotionSpace one = promotionSpaceService.getOne(queryWrapper);

        return ConvertUtil.convert(one,PromotionSpaceDTO.class);
    }

    @Override
    public ResponseDTO saveOrUpdateSpace(PromotionSpaceDTO promotionSpaceDTO) {
        PromotionSpace promotionSpace = ConvertUtil.convert(promotionSpaceDTO, PromotionSpace.class);
        Date date = new Date();
        if (null == promotionSpace.getId() ) {
            promotionSpace.setCreateTime(date);
            promotionSpace.setUpdateTime(date);
            promotionSpace.setIsDel(0);
        }else {
            promotionSpace.setUpdateTime(date);
        }
        ResponseDTO responseDTO = null;
        try {
            promotionSpaceService.saveOrUpdate(promotionSpace);
            responseDTO = ResponseDTO.success();
        } catch (Exception e) {
            responseDTO = ResponseDTO.ofError(e.getMessage());
        }
        return responseDTO;
    }
}
